﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using EnvDTE;
using System.IO;

namespace RuleCode.ObjectTree
{
    public partial class ConfigureUserControl : UserControl
    {
        #region 插件对象属性
        /// <summary>
        /// 获取或设置 插件宿主对象
        /// </summary>
        public DTE AddIn_ApplicationObject
        {
            get;
            set;
        }

        /// <summary>
        /// 获取或设置 插件对象中对应的窗口对象
        /// </summary>
        public Window AddIn_VSWindow
        {
            get;
            set;
        }
        #endregion

        #region 供插件调用的方法
        /// <summary>
        /// 初始化窗体,供插件调用
        /// </summary>
        public void InitForm()
        {
            InitTreeView();
            InitTabPage2();
            InitTaPage_Team();
        }

        #endregion

        XmlDocument aots;
        XmlNode aotsnode;
        string currentConfigName = string.Empty;

        public ConfigureUserControl()
        {
            InitializeComponent();
        }

       

        private void ConfigureUserControl_Load(object sender, EventArgs e)
        {                                                                        
        }     

        #region 数据初始化
        protected void InitTreeView()
        {
            //if (this.AddIn_ApplicationObject == null || this.AddIn_ApplicationObject.Solution.Count <= 0)
            //    return;
                        
            currentConfigName = Util.GetCurrentProfileName();
            TextBox_CurrentName.Text = currentConfigName;
            TextBox_CurrentConfigName_2.Text = currentConfigName;
            if (currentConfigName == string.Empty)
            {
                MessageBox.Show("您当前没有任何配置文件，请您务必做好配置！");
            }

            aots = new XmlDocument();
            aots.Load(Util.GetProfilePath());
            aotsnode = aots.SelectSingleNode("//aots");
            XmlNodeList nodes = aots.SelectNodes("//aot");
            TreeView_AllConfig.Nodes.Clear();
            foreach (XmlNode node in nodes)
            {
                string name = string.Empty;
                try
                {
                    name = node.Attributes.GetNamedItem("Name").Value;                                     
                }
                catch { }
                if (!String.IsNullOrEmpty(name))
                {
                    TreeNode treeNode = new TreeNode(name);
                    treeNode.ToolTipText = "右键更新";
                    TreeView_AllConfig.Nodes.Add(treeNode);
                    if (name == currentConfigName)
                    {
                        TreeView_AllConfig.SelectedNode = treeNode;
                    }
                }
            }
             
        }

        protected void InitTabPage2()
        {
            if (AddIn_ApplicationObject != null && AddIn_ApplicationObject.Solution.Count > 0)
            {
                TextBox_SolutionName_2.Text = AddIn_ApplicationObject.Solution.FileName;
                TextBox_SolutionPath_2.Text = AddIn_ApplicationObject.Solution.FullName;

                 
                Util.InitCurrentProfileName();
                CheckBox_CopyInCompile.Checked = Util.IsLabelEditAfterAddNewNode;
                TextBox_ModuleRootName_2.Text = Util.RootClassNameSpace;
                TextBox_FormRootName_2.Text = Util.RootFormNameSpace;
                TextBox_WebFormRootName_2.Text = Util.RootWebFormNameSpace;

                ComboBox[] AllProjectComboxs = new ComboBox[] {ComboBox_EDTProject_2 ,ComboBox_EnumProject_2 ,ComboBox_FormProject_2,
                    ComboBox_TableFieldProject_2 , ComboBox_TableProject_2 ,ComboBox_WebFormProject_2 };
                foreach (ComboBox comboBox in AllProjectComboxs)
                {
                    comboBox.Items.Clear();
                    comboBox.Items.Add("请选择项目");
                    for (int i = 1; i <= Util_DTE.AddIn_ApplicationObject.Solution.Projects.Count; i++)
                    {
                        string name = Util_DTE.AddIn_ApplicationObject.Solution.Projects.Item(i).Name;
                        if(name !="杂项文件")
                            comboBox.Items.Add(name);
                    }
                    comboBox.SelectedIndex = 0;
                }

                Struct_GenerateTypePathProject gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.ExtendDataType];
                TextBox_EDTPath_2.Text = gStruct.GeneratePath;
                ComboBox_EDTProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";
                    
                gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.Enum  ];
                TextBox_EnumPath_2.Text = gStruct.GeneratePath;
                this.ComboBox_EnumProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";

                gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.Form ];
                this.TextBox_FormPath_2 .Text = gStruct.GeneratePath;
                this.ComboBox_FormProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";

                gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.Table  ];
                this.TextBox_TablePath_2 .Text = gStruct.GeneratePath;
                this.ComboBox_TableProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";

                gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.TableField ];
                this.TextBox_TableFieldPath_2 .Text = gStruct.GeneratePath;
                this.ComboBox_TableFieldProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";

                gStruct = Util_DTE.AllGenerateTypePathProjects[GenerateFileType.WebForm]; 
                this.TextBox_WebFormPath_2  .Text = gStruct.GeneratePath;
                ComboBox_WebFormProject_2.SelectedItem = (gStruct.Project != null) ? gStruct.Project.Name : "请选择项目";
                

            }
        }

        private void InitTaPage_Team()
        {
            CheckBox_Team_AddFileToSolution.Checked = Util_DTE.IsAddLabelAndAOTToSolution;

            ComboBox_Project_Team_LabelFile.Items.Clear();
            ComboBox_Project_Team_ObjectTree.Items.Clear();
            ComboBox_Project_Team_LabelFile.Items.Add("请选择项目");
            ComboBox_Project_Team_ObjectTree.Items.Add("请选择项目");
            if (AddIn_ApplicationObject != null && AddIn_ApplicationObject.Solution.Count > 0)
            {
                for (int i = 1; i <= AddIn_ApplicationObject.Solution.Projects.Count; i++)
                {
                    if (AddIn_ApplicationObject.Solution.Projects.Item(i).Name != "杂项文件")
                    {
                        ComboBox_Project_Team_LabelFile.Items.Add(AddIn_ApplicationObject.Solution.Projects.Item(i).Name);
                        ComboBox_Project_Team_ObjectTree.Items.Add(AddIn_ApplicationObject.Solution.Projects.Item(i).Name);
                    }
                }
            }

            if (Util_DTE.AllFileVersionStruct != null && Util_DTE.AllFileVersionStruct.Count > 0)
            {
                if (Util_DTE.AllFileVersionStruct.Keys.Contains(FileVersionType.AOTFile))
                {
                    ComboBox_Project_Team_ObjectTree.SelectedItem = Util_DTE.AllFileVersionStruct[FileVersionType.AOTFile].Project.Name;
                    TextBox_ObjectTree_Team.Text = Util_DTE.AllFileVersionStruct[FileVersionType.AOTFile].GeneratePath;
                }
                else
                    ComboBox_Project_Team_ObjectTree.SelectedItem = "请选择项目";

                if (Util_DTE.AllFileVersionStruct.Keys.Contains(FileVersionType.LabelFile))
                {
                    ComboBox_Project_Team_LabelFile.SelectedItem = Util_DTE.AllFileVersionStruct[FileVersionType.LabelFile].Project.Name;
                    TextBox_LabelFile_Team.Text = Util_DTE.AllFileVersionStruct[FileVersionType.LabelFile].GeneratePath;
                }
                else
                    ComboBox_Project_Team_LabelFile.SelectedItem = "请选择项目";
            }

            ComboBox_Project_Team_ObjectTree.Enabled = CheckBox_Team_AddFileToSolution.Checked;
            ComboBox_Project_Team_LabelFile.Enabled = CheckBox_Team_AddFileToSolution.Checked;
            TextBox_ObjectTree_Team.ReadOnly = !CheckBox_Team_AddFileToSolution.Checked;
            TextBox_LabelFile_Team.ReadOnly = !CheckBox_Team_AddFileToSolution.Checked; 
        }
        #endregion

        #region 事件

         
        #endregion

        private void TreeView_AllConfig_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (TreeView_AllConfig.SelectedNode != null)
            {
                string curName = TreeView_AllConfig.SelectedNode.Text.ToString();
                aots = new XmlDocument();
                aots.Load(Util.GetProfilePath());
                aotsnode = aots.SelectSingleNode("//aots");
                XmlNode node = aots.SelectSingleNode("//aot[@Name='" + curName + "']");
                if (node != null)
                {
                    try
                    {
                        TextBox_Name.Text = node.Attributes.GetNamedItem("Name").Value;
                    }
                    catch { }
                    try
                    {
                        this.TextBox_OTPath.Text = node.Attributes.GetNamedItem("OTPath").Value;
                    }
                    catch { }
                    try
                    {
                        this.TextBox_ModulePath.Text = node.Attributes.GetNamedItem("OTPath_Module").Value;
                    }
                    catch { }
                    try
                    {
                        string cnnStringID = node.Attributes.GetNamedItem("cnnStringID").Value;
                        this.TextBox_SqlConnectStr.Text = Util.GetConnectString(cnnStringID) ;
                    }
                    catch { }
                }
            }
        }

        private void TreeView_AllConfig_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeView_AllConfig.SelectedNode = e.Node;
        }

        #region 右键菜单事件
        private void 选择为当前配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TreeView_AllConfig.SelectedNode != null)
            {
                Util.SetCurrentProfileName(TreeView_AllConfig.SelectedNode.Text);
                Util.SetBack();
                Util.InitCurrentProfileName();
                Util.InitOTPath();
                TextBox_CurrentName.Text = TreeView_AllConfig.SelectedNode.Text;
                TextBox_CurrentConfigName_2.Text = TreeView_AllConfig.SelectedNode.Text;
                MessageBox.Show("设置成功！");      
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TreeView_AllConfig.SelectedNode != null)
            {
                UpdateConfigureForm updateConfigForm = new UpdateConfigureForm();
                updateConfigForm.IsNew = false;
                updateConfigForm.CurrentConfigureName = TreeView_AllConfig.SelectedNode.Text;
                updateConfigForm.ShowDialog();
                InitTreeView();
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (TreeView_AllConfig.Nodes.Count <= 1)
            {
                MessageBox.Show("该配置不能删除，必须保留一个配置作为当前配置！");
                return;
            }

            if (TreeView_AllConfig.SelectedNode != null)
            {     
                string curName = TreeView_AllConfig.SelectedNode.Text;
                if (curName == currentConfigName)
                {
                    MessageBox.Show("当前配置不能删除！");
                    return;
                }
                else
                {
                    string solutionsFileName = Util.GetSolutionConfigPath();
                    XmlDocument solutionsDoc = new XmlDocument();
                    solutionsDoc.Load(solutionsFileName);
                    XmlNode soluNode = solutionsDoc.SelectSingleNode("//solution[@ProfileName='"+curName +"']");
                    if (soluNode != null)
                    {
                        string errorStr =string.Empty ;
                        XmlNode soluPathAttr = soluNode.Attributes.GetNamedItem("Path");
                        if (soluPathAttr != null)
                        {
                            errorStr = "项目："+soluPathAttr .Value +" 在引用配置："+curName +"！";
                        }
                        MessageBox.Show("您要删除的配置有项目在引用，不能删除！\n"+errorStr );
                        return;
                    }
                }

                if (MessageBox.Show("您确定要删除该配置吗？") == DialogResult.OK)
                {
                    aots = new XmlDocument();
                    aots.Load(Util.GetProfilePath());
                    aotsnode = aots.SelectSingleNode("//aots");
                    XmlNode node = aots.SelectSingleNode("//aot[@Name='" + curName + "']");
                    if (node != null)
                    {
                        XmlNode otModulePathAttr = node.Attributes.GetNamedItem("OTPath_Module");
                        XmlNode otPathAttr = node.Attributes.GetNamedItem("OTPath");

                        string configPath = Path.Combine(Path.Combine(Util.GetUserDataPath(), GlobalStaticDirectoryPath.AOTSProfilePath.Trim("\\".ToCharArray ())), curName);
                        if (Directory.Exists(configPath))
                            Directory.Delete(configPath,true );
                        aotsnode.RemoveChild(node);
                        aots.Save(Util.GetProfilePath());
                    }
                    InitTreeView();
                }
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdateConfigureForm updateConfigForm = new UpdateConfigureForm();
            updateConfigForm.IsNew = true;
            updateConfigForm.ShowDialog();
            InitTreeView();
        }
        #endregion

        private void Button_Cancel_Click(object sender, EventArgs e)
        {
            if (AddIn_ApplicationObject == null)
                this.ParentForm.Close();
            else
            {
                if (AddIn_VSWindow != null)
                    AddIn_VSWindow.Close(vsSaveChanges.vsSaveChangesPrompt);
            }
        }

        private void Button_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.AddIn_ApplicationObject != null && this.AddIn_ApplicationObject.Solution.Count > 0)
                {

                    string solutionName = this.AddIn_ApplicationObject.Solution.FullName;              
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(Util.GetSolutionConfigPath());
                    XmlNode solutionNode = xmlDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']");
                    if (solutionNode != null)
                    {
                        XmlNode pathNode = solutionNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths");
                        XmlNode childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//Tables");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (ComboBox_TableProject_2.SelectedIndex != 0) ? ComboBox_TableProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = TextBox_TablePath_2.Text;
                        }
                        childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//TableFields");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (this.ComboBox_TableFieldProject_2.SelectedIndex != 0) ? ComboBox_TableFieldProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = this.TextBox_TableFieldPath_2.Text;
                        }
                        childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//ExtendDataTypes");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (this.ComboBox_EDTProject_2.SelectedIndex != 0) ? ComboBox_EDTProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = this.TextBox_EDTPath_2.Text;
                        }
                        childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//Enums");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (this.ComboBox_EnumProject_2.SelectedIndex != 0) ? ComboBox_EnumProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = this.TextBox_EnumPath_2.Text;
                        }
                        childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//WebForms");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (this.ComboBox_WebFormProject_2.SelectedIndex != 0) ? ComboBox_WebFormProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = this.TextBox_WebFormPath_2.Text;
                        }
                        childNode = pathNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//paths" + "//Forms");
                        if (childNode != null)
                        {
                            childNode.Attributes.GetNamedItem("Project").Value = (this.ComboBox_FormProject_2.SelectedIndex != 0) ? ComboBox_FormProject_2.SelectedItem.ToString() : string.Empty;
                            childNode.Attributes.GetNamedItem("Value").Value = this.TextBox_FormPath_2.Text;
                        }

                        XmlNode rootNamespaceNode = solutionNode.SelectSingleNode("//RootNameSpace");
                        XmlNode childRootNode = rootNamespaceNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//RootNameSpace//Classes");
                        if (childRootNode != null)
                            childRootNode.InnerText = TextBox_ModuleRootName_2.Text;
                        childRootNode = rootNamespaceNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//RootNameSpace//WebForms");
                        if (childRootNode != null)
                            childRootNode.InnerText = this.TextBox_WebFormRootName_2.Text;
                        childRootNode = rootNamespaceNode.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "//RootNameSpace//Forms");
                        if (childRootNode != null)
                            childRootNode.InnerText = this.TextBox_FormRootName_2.Text;
                        xmlDoc.Save(Util.GetSolutionConfigPath());

                        //在WebForm窗体的生成路径下面增加相应的文件夹
                        string webFormProjectName = ComboBox_WebFormProject_2.SelectedIndex != 0 ? ComboBox_WebFormProject_2.SelectedItem.ToString() : string.Empty;
                        if (!String.IsNullOrEmpty(webFormProjectName))
                        {
                            Project webFormProject = Util_DTE.FindProject(webFormProjectName);
                            string webFormPath = TextBox_WebFormPath_2.Text.TrimEnd("\\".ToCharArray()); ;
                            if (webFormProject != null)
                            {
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormMasterSlaves);
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormParaByUsers);
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormParameters);
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormRelations);
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormSingles);
                                Util_DTE.FindProjectItem(webFormProject, webFormPath + "\\" + OT.WebFormTrees);
                                Util_DTE.AddIn_ApplicationObject.Solution.SaveAs(Util_DTE.AddIn_ApplicationObject.Solution.FullName);
                            }
                        }

                        MessageBox.Show("修改成功！");
                        Util.SetBack();
                        Util.InitCurrentProfileName();
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("修改失败！");
                Util.SetStatusInfo(error.Message);
            }
        }

        private void Button_Close_Click(object sender, EventArgs e)
        {
            InitForm();
        }

        private void TextBox_EnumPath_2_Enter(object sender, EventArgs e)
        {

        }

        #region 辅助方法
        private string _CurrentSolutionPath = null ;
        private string CurrentSolutionPath
        {
            get
            {
                if (_CurrentSolutionPath == null)
                    _CurrentSolutionPath = GetCurrentSolutionPath();
                return _CurrentSolutionPath;
            }
        }

        private bool IsChildPath(string parentPath, string childPath)
        {
            bool result = false;
            DirectoryInfo parentDInfo = new DirectoryInfo(parentPath);
            DirectoryInfo[] allChildDInfos = parentDInfo.GetDirectories();
            foreach (DirectoryInfo childDInfo in allChildDInfos)
            {
                if (childDInfo.FullName == childPath)
                    return true;

                if(IsChildPath(childDInfo .FullName ,childPath ))
                    return true ;
            }
            return result;
        }

        private string GetCurrentSolutionPath()
        {
            string path = string.Empty;
            if (AddIn_ApplicationObject != null && AddIn_ApplicationObject.Solution.Count > 0)
            {
                DirectoryInfo dInfo = new DirectoryInfo(AddIn_ApplicationObject.Solution.FullName);
                path = dInfo.Parent .FullName;
            }
            return path;
        }
        #endregion

        #region TabPage_Team
        

        private void Button_Team_Cancel_Click(object sender, EventArgs e)
        {
            CheckBox_Team_AddFileToSolution.Checked = false;
        }

        private void Button_Team_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (AddIn_ApplicationObject != null && AddIn_ApplicationObject.Solution.Count > 0)
                {
                    string solutionName = AddIn_ApplicationObject.Solution.FullName;

                    string solutionXmlFileName = Util.GetSolutionConfigPath();
                    XmlDocument solutionDoc = new XmlDocument();
                    solutionDoc.Load(solutionXmlFileName);
                    XmlNode solutionNode = solutionDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']");
                    XmlNode solutionPathNode = solutionDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']/paths");
                    if (solutionNode != null && solutionPathNode!=null)
                    {
                        XmlNode addSubAttr = solutionNode.Attributes.GetNamedItem("AddToSubvesion");
                        if (addSubAttr == null)
                        {
                            addSubAttr = solutionDoc.CreateAttribute("AddToSubvesion");
                            solutionNode.Attributes.Append(addSubAttr as XmlAttribute);
                        }
                        addSubAttr.Value = CheckBox_Team_AddFileToSolution.Checked ? "true" : "false";

                        XmlNode labelNode = solutionDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "/paths/LabelFile");
                        if (labelNode == null)
                        {
                            labelNode = solutionDoc.CreateElement("LabelFile");
                            solutionPathNode.AppendChild(labelNode);
                        }
                        XmlNode attr1 = labelNode.Attributes.GetNamedItem("Project");
                        if (attr1 == null)
                        {
                            attr1 = solutionDoc.CreateAttribute("Project");
                            labelNode.Attributes.Append(attr1 as XmlAttribute);
                        }
                        attr1.Value = ComboBox_Project_Team_LabelFile.Text == "请选择项目" ? "" : ComboBox_Project_Team_LabelFile.Text;

                        XmlNode attr2 = labelNode.Attributes.GetNamedItem("Value");
                        if (attr2 == null)
                        {
                            attr2 = solutionDoc.CreateAttribute("Value");
                            labelNode.Attributes.Append(attr2 as XmlAttribute);
                        }
                        attr2.Value = TextBox_LabelFile_Team.Text.Trim();

                        XmlNode aotNode = solutionDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']" + "/paths/AOTFile");
                        if (aotNode == null)
                        {
                            aotNode = solutionDoc.CreateElement("AOTFile");
                            solutionPathNode.AppendChild(aotNode);
                        }

                        XmlNode attr3 = aotNode.Attributes.GetNamedItem("Project");
                        if (attr3 == null)
                        {
                            attr3 = solutionDoc.CreateAttribute("Project");
                            aotNode.Attributes.Append(attr3 as XmlAttribute);
                        }
                        attr3.Value = this.ComboBox_Project_Team_ObjectTree.Text == "请选择项目" ? "" : ComboBox_Project_Team_ObjectTree.Text;

                        XmlNode attr4 = aotNode.Attributes.GetNamedItem("Value");
                        if (attr4 == null)
                        {
                            attr4 = solutionDoc.CreateAttribute("Value");
                            aotNode.Attributes.Append(attr4 as XmlAttribute);
                        }
                        attr4.Value = this.TextBox_ObjectTree_Team.Text.Trim();
                    }
                                                                                         
                    if (CheckBox_Team_AddFileToSolution.Checked)
                    {
                        //拷贝LabelFile到项目中去
                        string oldLabelFilePath = Util_LabelEditor.Instance.LabelFilePath;
                        string newLabelNodePath = TextBox_LabelFile_Team.Text.Trim();
                        string labelProjectName = ComboBox_Project_Team_LabelFile.Text == "请选择项目" ? "" : ComboBox_Project_Team_LabelFile.Text;
                        newLabelNodePath = newLabelNodePath.TrimEnd("\\".ToCharArray()) + "\\LabelFile";
                        if (labelProjectName != string.Empty)
                        {
                            Project labelProject = Util_DTE.FindProject(labelProjectName);
                            if (labelProject != null)
                            {
                                //判断是否和原来的是一样的
                                FileInfo projectInfo = new FileInfo(labelProject.FullName);
                                string labelProjectPath = Path.Combine(projectInfo.DirectoryName, newLabelNodePath.Trim("\\".ToCharArray ()));
                                if (labelProjectPath.ToLower() != oldLabelFilePath.ToLower())
                                {
                                    DirectoryInfo labelFileDInfo = new DirectoryInfo(oldLabelFilePath);
                                    FileInfo[] allLabelFilesInfo = labelFileDInfo.GetFiles();
                                    foreach (FileInfo fInfo in allLabelFilesInfo)
                                    {
                                        string sourceFileName = fInfo.FullName;
                                        Util_DTE.AddGeneratedFileToProject(labelProject, newLabelNodePath, sourceFileName);
                                    }
                                }
                            }
                        }

                        //拷贝对象树的属性文件到项目中去
                        string oldObjPath = Util.GetObjDataPath;
                        string objParentPath = TextBox_ObjectTree_Team.Text.Trim().TrimEnd("\\".ToCharArray());
                        string newObjPath = objParentPath + GlobalStaticDirectoryPath.ObjectData;
                        string objProjectName = ComboBox_Project_Team_ObjectTree.Text == "请选择项目" ? "" : ComboBox_Project_Team_ObjectTree.Text.Trim();


                        //拷贝 aotFile.xml到项目中去
                        string oldObjParentPath = oldObjPath.TrimEnd(GlobalStaticDirectoryPath.ObjectData.ToCharArray());
                        string aotFileName = Path.Combine(oldObjParentPath, "aotFile.xml");
                        string aotFileBakName = Path.Combine(oldObjParentPath, "aotFile.xml.bak");

                        if (!String.IsNullOrEmpty(objProjectName))
                        {
                            Project objProject = Util_DTE.FindProject(objProjectName);
                            if (objProject != null)
                            {
                                //判断路径是否和原来的一样
                                FileInfo objProjectInfo = new FileInfo(objProject.FullName);
                                string objProjectPath = Path.Combine(objProjectInfo.DirectoryName, newObjPath.Trim("\\".ToCharArray ()));
                                if (oldObjPath.ToLower() != objProjectPath.ToLower())
                                {

                                    DirectoryInfo objFileDInfo = new DirectoryInfo(oldObjPath);
                                    FileInfo[] allObjFilesInfo = objFileDInfo.GetFiles();
                                    foreach (FileInfo fInfo in allObjFilesInfo)
                                    {
                                        string sourceFileName = fInfo.FullName;
                                        Util_DTE.AddGeneratedFileToProject(objProject, newObjPath, sourceFileName);
                                    }

                                    Util_DTE.AddGeneratedFileToProject(objProject, objParentPath, aotFileName);
                                    Util_DTE.AddGeneratedFileToProject(objProject, objParentPath, aotFileBakName);
                                }
                            }
                        }

                    }
                    else //设置为默认
                    {
                        //将LabelFile拷贝到默认的文件夹下面
                        string oldLabelFilePath = Util_LabelEditor.Instance.LabelFilePath;
                        string newLabelNodePath = Path.Combine (Util.OTPath , "LabelFile");

                        if (newLabelNodePath.ToLower() != oldLabelFilePath.ToLower())
                        {
                            DirectoryInfo labelFileDInfo = new DirectoryInfo(oldLabelFilePath);
                            FileInfo[] allLabelFilesInfo = labelFileDInfo.GetFiles();
                            if (!Directory.Exists(newLabelNodePath))
                                Directory.CreateDirectory(newLabelNodePath);
                            foreach (FileInfo fInfo in allLabelFilesInfo)
                            {
                                if (File.Exists(newLabelNodePath + "\\" + fInfo.Name))
                                    File.Delete(newLabelNodePath + "\\" + fInfo.Name);
                                fInfo.CopyTo(newLabelNodePath + "\\" + fInfo.Name);
                            }
                        }

                        //拷贝对象树的属性文件到默认文件夹去
                        string oldObjPath = Util.GetObjDataPath;
                        string newObjParentPath = Util.OTPath.TrimEnd("\\".ToCharArray());
                        string newObjPath = newObjParentPath + GlobalStaticDirectoryPath.ObjectData;    

                        //拷贝 aotFile.xml到项目中去
                        string oldObjParentPath = oldObjPath.TrimEnd(GlobalStaticDirectoryPath.ObjectData.ToCharArray());

                        string aotFileName = Path.Combine(oldObjParentPath, "aotFile.xml");
                        string aotFileBakName = Path.Combine(oldObjParentPath, "aotFile.xml.bak");

                        if (oldObjPath.ToLower() != newObjPath.ToLower())
                        {

                            DirectoryInfo objFileDInfo = new DirectoryInfo(oldObjPath);
                            FileInfo[] allObjFilesInfo = objFileDInfo.GetFiles();
                            if (!Directory.Exists(newObjPath))
                                Directory.CreateDirectory(newObjPath);
                            foreach (FileInfo fInfo in allObjFilesInfo)
                            {
                                if (File.Exists(newObjPath + "\\" + fInfo.Name))
                                    File.Delete(newObjPath + "\\" + fInfo.Name);
                                fInfo.CopyTo(newObjPath + "\\" + fInfo.Name);
                            }

                            //if(File.Exists (oldObjParentPath + "\\" + "aotFile.xml"))
                                File.Copy(oldObjParentPath + "\\" + "aotFile.xml", newObjParentPath + "\\aotFile.xml",true );

                            //if (File.Exists(oldObjParentPath + "\\" + "aotFile.xml.bak"))
                                File.Copy(oldObjParentPath + "\\" + "aotFile.xml.bak", newObjParentPath + "\\aotFile.xml.bak", true);
                        }
                    }
                    solutionDoc.Save(solutionXmlFileName);
                    string oldAOTFilePath = Util.GetObjDataPath;

                    //重新初始化配置  
                    Util.SetBack();
                    Util.InitCurrentProfileName();

                    MessageBox.Show("设置成功！");
                    Util.SetStatusInfo("设置成功！");
                }
            }
            catch (Exception error)
            {
                Util.SetStatusInfo(error .Message );
                MessageBox.Show("设置失败："+error .Message );
            }
        }

        private void TabPage_Team_Click(object sender, EventArgs e)
        {

        }
        #endregion

        private void CheckBox_Team_AddFileToSolution_CheckedChanged(object sender, EventArgs e)
        {                                                    
             //InitTaPage_Team();


            ComboBox_Project_Team_ObjectTree.Enabled = CheckBox_Team_AddFileToSolution .Checked ;
            ComboBox_Project_Team_LabelFile.Enabled = CheckBox_Team_AddFileToSolution.Checked;
            TextBox_ObjectTree_Team.ReadOnly = !CheckBox_Team_AddFileToSolution.Checked;
            TextBox_LabelFile_Team.ReadOnly = !CheckBox_Team_AddFileToSolution.Checked; 
        }

        private void Button_NewConfig_Click(object sender, EventArgs e)
        {
            添加ToolStripMenuItem_Click(添加ToolStripMenuItem, EventArgs.Empty);
        }

       
    }
}
